﻿using System;
using System.Collections.Generic;

namespace BuzzX8.Xmpp.Server
{
    public class CommandProcessor
    {
        private IDictionary<int, CommandHandler> handlers;
        private XmppServer commandTarget;

        public CommandProcessor(XmppServer commandTarget)
        {
            if(commandTarget == null) throw new ArgumentNullException("commandTarget");
            this.commandTarget = commandTarget;
            handlers = new Dictionary<int, CommandHandler>();
        }

        public void AddHandler(int commandId, CommandHandler handler)
        {
            if(handler == null) throw new ArgumentNullException("handler");
            if (handlers.ContainsKey(commandId)) handlers[commandId] += handler;
            else handlers.Add(commandId, handler);
        }

        public void Execute(Command command)
        {
            if (handlers.ContainsKey(command.Id)) handlers[command.Id](commandTarget, command);
        }
    }

    public delegate void CommandHandler(XmppServer commandTarget, Command command);
}
